﻿using BTL_Cloud.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BaseSubject;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BaseSubjectRepository : BaseRepository<Base_SubjectInfo>,IBaseSubjectRepository
    {
        public BaseSubjectRepository(AppBookShopContext myContext,
        IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
            currentDbSet = _myContext.BaseSubjectInfo;
        }

        public async Task<PaginatedList<Base_SubjectInfo>> GetAllEntitiesAsync(BaseSubjectQueryParameters parameters)
        {
            var query = currentDbSet.AsQueryable();

            if (!string.IsNullOrEmpty(parameters.keyword))
            {
                string keyword = parameters.keyword.ToLowerInvariant();
                query= query.Where(n => n.SubjectName.ToLowerInvariant().Contains(keyword));
            }

            query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve<BaseSubjectResource, Base_SubjectInfo>());

            var count = await query.CountAsync();
            var data = await query
                .Skip(parameters.PageIndex * parameters.PageSize)
                .Take(parameters.PageSize)
                .ToListAsync();

            return new PaginatedList<Base_SubjectInfo>(parameters.PageIndex, parameters.PageSize, count, data);
        }

        //public async Task<Base_SubjectInfo> GetEntityByIdAsync(string id)
        //{
        //    return await _myContext.BaseSubjectInfo.FindAsync(id);
        //}

        //public void Add(Base_SubjectInfo entity)
        //{
        //    _myContext.BaseSubjectInfo.Add(entity);
        //}

        //public void Delete(Base_SubjectInfo entity)
        //{
        //    _myContext.BaseSubjectInfo.Remove(entity);
        //}

        //public void Update(Base_SubjectInfo entity)
        //{
        //    _myContext.Entry(entity).State = EntityState.Modified;
        //}
    }
}
